﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;

namespace Bus_proj.website
{
    public partial class login : System.Web.UI.Page
    {
        string Userdata, Passdata, identicationdata;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session.Remove("login_User");
                Session.Remove("login_Access_id");
            }
        }

        public static string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        public DataTable GetDataPassenger(string U, string p)
        {
            DataTable dt = new DataTable();
            string chk_pass = getMd5Hash(p);
            string sqltext = "SELECT * FROM Table_Passenger WHERE Passenger_identification = '" + U + "' and password ='" + chk_pass + "'; ";
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }

        public DataTable GetDataPersonnel(string U, string p)
        {
            DataTable dt = new DataTable();
            string chk_pass = getMd5Hash(p);
            string sqltext = "SELECT * FROM Table_Personnel WHERE Personnel_identification = '" + U + "' and Personnel_pass ='" + chk_pass + "'; ";
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }

        public DataTable GetDataManager(string U, string p)
        {
            DataTable dt = new DataTable();
            string chk_pass = getMd5Hash(p);
            string sqltext = "SELECT * FROM Table_Manager WHERE Manager_id = '" + U + "' and Manager_pass ='" + chk_pass + "'; ";
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da_admin = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da_admin.Fill(ds);
            return ds.Tables[0];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Username = TextBox1.Text;
            string Password = TextBox2.Text;
            DataTable dt_Passenger = GetDataPassenger(Username, Password);
            DataTable dt_Personnel = GetDataPersonnel(Username, Password);
            DataTable dt_Manager = GetDataManager(Username, Password);
            if (dt_Passenger != null && dt_Passenger.Rows.Count > 0)
            {
                Userdata = dt_Passenger.Rows[0]["Passenger_id"].ToString();
                Passdata = dt_Passenger.Rows[0]["Access_id"].ToString();
                identicationdata = dt_Passenger.Rows[0]["Passenger_identification"].ToString();
                Session["login_User"] = Userdata;
                Session["login_Access_id"] = Passdata;
                Session["login_Passenger_identification"] = identicationdata;
                Response.Redirect("~/website/home_user.aspx");
            }            
            else if (dt_Personnel != null && dt_Personnel.Rows.Count > 0)
            {
                Userdata = dt_Personnel.Rows[0]["Personnel_id"].ToString();
                Passdata = dt_Personnel.Rows[0]["Access_id"].ToString();
                identicationdata = dt_Personnel.Rows[0]["Personnel_identification"].ToString();
                Session["login_User"] = Userdata;
                Session["login_Access_id"] = Passdata;
                Session["login_Personnel_identification"] = identicationdata;
                Response.Redirect("~/website/home_personnel.aspx");
            }            
            else if (dt_Manager != null && dt_Manager.Rows.Count > 0)
            {
                Userdata = dt_Manager.Rows[0]["Manager_id"].ToString();
                Passdata = dt_Manager.Rows[0]["Access_id"].ToString();
                Session["login_User"] = Userdata;
                Session["login_Access_id"] = Passdata;
                Response.Redirect("~/website/home_manager.aspx");
            }
            else
            {
                Label3.Visible = true;
            }
        }
       
    }
}